package cn.wolfcode.luowowo.member.service.impl;

import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.common.util.AssertUtil;
import cn.wolfcode.luowowo.common.util.Consts;
import cn.wolfcode.luowowo.member.domain.Fans;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.mapper.UserInfoMapper;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

/**
 * Created by admin on 2019/8/7.
 */
@Service
public class UserInfoServiceImpl implements IUserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Reference
    private IUserInfoRedisService redisService;

    // 查询电话号码是否存在
    @Override
    public boolean selectCountByPhone(String phone) {
        return userInfoMapper.selectCountByPhone(phone) > 0;
    }

    // 发送验证码
    @Override
    public void sendVerifyCode(String phone) {
        // 生成验证码
        String code = UUID.randomUUID().toString().substring(0, 5);

        // 缓存
        redisService.setVerifyCode(phone, code);

        // 发送验证码
        StringBuilder sb = new StringBuilder(100);
        sb.append("您的验证码是:")
                .append(code)
                .append(",在")
                .append(Consts.VERIFY_CODE_VAI_TIME)
                .append("分钟内有效");
        System.out.println(sb);
    }

    // 注册验证
    @Override
    public void userRegist(String phone, String nickname, String password, String rpassword, String verifyCode) {

        AssertUtil.hasLength(phone, "电话号码不能为空");
        AssertUtil.hasLength(nickname, "昵称不能为空");
        AssertUtil.hasLength(password, "密码不能为空");
        AssertUtil.hasLength(rpassword, "确认密码不能为空");
        AssertUtil.hasLength(verifyCode, "验证码不能为空");

        AssertUtil.isEquals(rpassword, rpassword, "两次密码不相同");

        String redisCode = redisService.getVerifyCode(phone);

        AssertUtil.isEquals(verifyCode, redisCode, "验证码不正确或者超时");

        UserInfo userInfo = new UserInfo();
        userInfo.setNickname(nickname);

        // Md5Hash md5Password = new Md5Hash(password, nickname);

        //String md5Password = DigestUtils.md5DigestAsHex((password + phone).getBytes());
        String md5Password = password;
        userInfo.setPassword(md5Password);

        userInfo.setPhone(phone);
        userInfo.setLevel(0);
        userInfo.setState(UserInfo.GENDER_SECRET);
        userInfo.setHeadImgUrl("/images/default.jpg");

        userInfoMapper.insert(userInfo);
    }

    // 用户登录验证
    @Override
    public String userLogin(String username, String password) {
        // 判断账户和密码是否为空
        AssertUtil.hasLength(username, "账号不能为空");
        AssertUtil.hasLength(password, "密码不能为空");
        // 查询出是否存在该用户

        //
        // password = DigestUtils.md5DigestAsHex((password + username).getBytes());

        UserInfo userInfo = userInfoMapper.selectUserInfoByUsernameAndPassword(username, password);

        // 如果不存在,则抛出异常
        if (userInfo == null) {
            throw new LogicException("账户或者密码错误");
        }

        // 创建一个唯一的token
        String token = UUID.randomUUID().toString().replace("-", "");

        // 将token作为key userInfo作为value 存入到redis中
        redisService.setUserInfoToken(token, userInfo);

        return token;
    }

    @Override
    public List<UserInfo> list() {
        return userInfoMapper.selectAll();
    }

    public void update(UserInfo user) {
         userInfoMapper.update(user);
    }

    public List<Fans> listByUserId(Long id) {
        return userInfoMapper.listByUserId(id);
    }

    public List<UserInfo> listByFansId(long id) {
        return userInfoMapper.listByFansId(id);
    }

    public void updateHeadImgUrl(String headImgUrl, Long id) {
        userInfoMapper.updateHeadImgUrl(headImgUrl,id);
    }

    public UserInfo get(Long id) {
        return userInfoMapper.selectByPrimaryKey(id);
    }

    public UserInfo checkPhoneNum(String phone, String oldpassword) {
        return userInfoMapper.selectByPhoneNum(phone,oldpassword);
    }

    public void updatePassword(Long id, String newpassword) {
        userInfoMapper.updatePassword(id,newpassword);
    }

    public void updateEmail(Long id, String email) {
        userInfoMapper.updateEmail(id,email);
    }

    public void updatePhone(Long id, String newphone) {
        userInfoMapper.updatePhone(id,newphone);
    }

    public List<UserInfo> state(int state) {
        return userInfoMapper.state(state);
    }

}
